package com.itheima.wisdomnext.mapper;

import com.itheima.wisdomnext.entity.Category;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CategoryMapper {

    // 根据分类 ID 查找分类
    @Select("SELECT * FROM categories WHERE id = #{id}")
    Category findById(int id);

    // 根据分类名称查找分类
    @Select("SELECT * FROM categories WHERE name = #{name}")
    Category findByName(String name);

    // 获取所有分类
    @Select("SELECT * FROM categories")
    List<Category> findAll();

    // 添加新的分类
    @Insert("INSERT INTO categories (name) VALUES (#{name})")
    @Options(useGeneratedKeys = true, keyProperty = "id")  // 自动生成主键
    void insert(Category category);

    // 检查分类是否存在
    @Select("SELECT COUNT(*) FROM categories WHERE name = #{name}")
    int existsByName(String name);

    // 删除分类
    @Delete("DELETE FROM categories WHERE id = #{id}")
    void deleteById(int id);

    // 更新分类
    @Update("UPDATE categories SET name = #{name} WHERE id = #{id}")
    int update(Category category);
}
